package cqrtplm.listener;

import cn.hutool.extra.spring.SpringUtil;
import com.hustcad.plm.pdm.workflow.annotion.CustomListener;
import com.hustcad.plm.pdm.workflow.annotion.CustomListenerField;
import com.hustcad.plm.pdm.workflow.model.entity.WorkFlowContainer;
import com.hustcad.plm.pdm.workflow.service.TyplmWorkFlowContainerService;
import cqrtplm.service.RTChangeNoticeService;
import cqrtplm.service.RTChgListenerService;
import org.apache.commons.lang3.StringUtils;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.ExecutionListener;
import org.flowable.engine.impl.el.FixedValue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.BigInteger;

@CustomListener(category = "润通", description = "设置ECN关联的所有变更前/后对象到目标状态")
public class SetChangeObjectStatusListener implements ExecutionListener {
    private static final Logger log = LoggerFactory.getLogger(SetChangeObjectStatusListener.class);
    private static final long serialVersionUID = -156096867698460146L;
    @CustomListenerField(description = "目标生命周期阶段key", defaultValue = "", required = false)
    private FixedValue targetStatusKey;
    @CustomListenerField(description = "变更前/变更后", defaultValue = "", required = false)
    private FixedValue tense;
    @CustomListenerField(description = "变更对象类型", defaultValue = "", required = false)
    private FixedValue objectType;

    public void notify(DelegateExecution delegateExecution) {
        try {
            log.info("开始执行监听 ==> SetChangeObjectStatusListener");
            String workFlowContainerId = delegateExecution.getProcessInstanceBusinessKey();
            if (StringUtils.isBlank(workFlowContainerId)) {
                workFlowContainerId = delegateExecution.getVariable("workFlowContainerId").toString();
            }
            TyplmWorkFlowContainerService typlmWorkFlowContainerService = (TyplmWorkFlowContainerService) SpringUtil.getBean(TyplmWorkFlowContainerService.class);
            WorkFlowContainer workFlowContainer = (WorkFlowContainer) typlmWorkFlowContainerService.selectOneByOid(new BigInteger(workFlowContainerId));
            BigInteger ecnId = workFlowContainer.getBaseObjectOid();
            if (this.targetStatusKey == null || this.objectType == null || this.tense == null) {
                log.info("SetChangeObjectStatusListener，监听参数targetStatusKey为空，不处理，请给对应监听参数设置值！");
            } else {
                RTChgListenerService rtChgListenerService = (RTChgListenerService) SpringUtil.getBean(RTChgListenerService.class);
                rtChgListenerService.setChangeObjectStatus(new BigInteger(workFlowContainerId), this.objectType.getExpressionText(), this.tense.getExpressionText(), this.targetStatusKey.getExpressionText());
                log.info("正常执行监听结束 <== SetChangeObjectStatusListener");
            }
        } catch (Exception e) {
            log.error("监听SetEcnRelevanceChgAfterObjectSpecifyStateListener异常：", e);
            throw e;
        }
    }
}
